package com.wqz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.fabric.Response;
import com.sun.deploy.net.HttpResponse;
import com.wqz.pojo.book;
import com.wqz.service.bookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("bookServiceImpl")
    private bookService bookService;


    @RequestMapping("/showBook")
    public String showBook(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page) {

        int pageSize = 4;//每页显示记录数
        //分页查询
        PageHelper.startPage(page, pageSize);
        List<book> list = bookService.queryAllBook();
        PageInfo<book> pageInfo = new PageInfo<book>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "allBook";
    }


    @RequestMapping("/toAddBook")
    public String toAddBook() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(book book) {
        bookService.addBook(book);
        return "allBook";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(int id, Model model) {
        book books = bookService.queryBookById(id);
        model.addAttribute("book", books);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(book book) {
        bookService.updateBook(book);
        return "allBook";
    }

    @RequestMapping("/deleteBook/{bookID}")
    public String deleteBookById(@PathVariable("bookID") int id) {
        bookService.deleteBookById(id);
        return "allBook";
    }

    @RequestMapping("/queryBookById")
    public String queryBookById(int bookID, Model model) {

        book books = bookService.queryBookById(bookID);
        System.err.println("queryBook=>" + books);
        List<book> list = new ArrayList<book>();
        list.add(books);
        if (books == null) {
            return "redirect:/book/empty";
        }
        PageInfo<book> pageInfo = new PageInfo<book>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "allBook";
    }

    @RequestMapping("/queryBookByName")
    public String queryBookByName(Model model, String bookName) {

        PageHelper.startPage(1, 4);
        List<book> list = bookService.queryBookByName(bookName);
        System.out.println("queryBook=>" + list);

        if (list.size() == 0) {
            return "redirect:/book/empty";
        }

        PageInfo<book> pageInfo = new PageInfo<book>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "allBook";
    }

    @RequestMapping("/empty")
    public String empty() {


        return "empty";
    }
}
